package com.epsoft.odin7cj.flow.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.DeploymentBuilder;

/**
 * 流程文件
 * @author Administrator
 *
 */
public class C04DeploymentBuilder {

	public static void main1(String[] args) throws Exception {
		
		// 流程引擎
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		
		// 获取存储服务
		RepositoryService rs = engine.getRepositoryService();
		
		DeploymentBuilder builder = rs.createDeployment();
		
		C04DeploymentBuilder builderDemo = new C04DeploymentBuilder();
		
		// 部署测试图片
//		builderDemo.deployTestImg(builder);
		
		// 通过 BpmnModel 代码实现部署
//		builderDemo.deployBpmnModel(builder);
		
		// 通过 bpmn 配置文件部署
		builderDemo.deployBpmnConfig(builder);
		
	}
	
	void deployBpmnConfig(DeploymentBuilder builder) {
		try {
			builder.addClasspathResource("bpmn/c04-deployment-build.bpmn");
			builder.deploy();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	void deployBpmnModel(DeploymentBuilder builder) {
		try {
			BpmnModel bpmnModel = new BpmnModel();
			
			// 创建一个流程定义
			org.activiti.bpmn.model.Process process = new org.activiti.bpmn.model.Process();
			process.setId("code-process");
			process.setName("My Code Process");
			
			// 创建开始事件
			StartEvent startEvent = new StartEvent();
			startEvent.setId("code-startEvent");
			startEvent.setName("My Code StartEvent");
			process.addFlowElement(startEvent);
			
			// 员工任务
			UserTask employeeTask = new UserTask();
			employeeTask.setId("employee-task");
			employeeTask.setName("Employee Task");
			process.addFlowElement(employeeTask);
			
			// 经理任务
			UserTask managerTask = new UserTask();
			managerTask.setId("manager-task");
			managerTask.setName("Manager Task");
			process.addFlowElement(managerTask);
			
			// 创建结束事件
			EndEvent endEvent = new EndEvent();
			endEvent.setId("code-endEvent");
			endEvent.setName("My Code EndEvent");
			process.addFlowElement(endEvent);
			
			// 连线
			process.addFlowElement(new SequenceFlow("code-startEvent","employee-task"));
			process.addFlowElement(new SequenceFlow("employee-task","manager-task"));
			process.addFlowElement(new SequenceFlow("manager-task","code-endEvent"));
			
			bpmnModel.addProcess(process);
			builder.addBpmnModel("codeSource", bpmnModel);
			builder.deploy();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	void deployTestImg(DeploymentBuilder builder) {
        InputStream inputStream;
		try {
			String filePath = this.getClass().getClassLoader().getResource("bpmn/test.jpg").getPath();
			inputStream = new FileInputStream(new File(filePath));
			builder.addInputStream("test 图片", inputStream);
			builder.deploy();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
}
